<template>
    <div class="map_container">
        <div v-if="loading_completed" class="map">
            <section style="width: 100%; height: 100%; float: right;">
                <client-only>
                    <el-amap
                        id="amap"
                        class="amap-demo"
                        :center="center"
                        :events="events"
                        :amap-manager="amapManager"
                    >
                        <el-amap-marker
                            v-for="(marker, index) in markers"
                            :key="index"
                            :ref="`marker${index}`"
                            :position="marker.position"
                            :vid="index"
                            :events="marker.events"
                            :content="marker.content"
                            :z-index="marker.zIndex"
                            animation="AMAP_ANIMATION_DROP"
                        />
                        <el-amap-info-window
                            :position="currentWindow.position"
                            :content="currentWindow.content"
                            :visible="currentWindow.visible"
                            :events="currentWindow.events"
                            :offset="currentWindow.offset"
                        />
                    </el-amap>
                </client-only>
            </section>
        </div>
        <div v-else class="loading">
            <img :src="require('@/static/loading.gif')" alt="" style="width: 100%; height: 100%">
        </div>
    </div>
</template>

<script>
    import VueAMap from "vue-amap";
    import * as _ from "lodash";
    const amapManager = new VueAMap.AMapManager();
    export default {
        props: {
            hotelDetailList: {
                require: true,
                type: Array,
                default () {
                    return [];
                }
            },
            isAround: {
                require: true,
                type: Boolean,
                default () {
                    return false;
                }
            },
            center_index: {
                require: false,
                type: Number,
                default () {
                    return 0;
                }
            },
            hotel_detail: {
                require: true,
                type: Object,
                default () {
                    return {}
                }    
            }
        },
        data () {
            const self = this;
            return {
                loading_completed: false,
                center: [],
                zooms: [3, 18],
                amapManager,
                events: {
                    init (map) {
                        const markers = _.cloneDeep(self.markers);
                        markers.forEach((item, index) => {
                            // eslint-disable-next-line no-undef
                            AMapUI.loadUI(["overlay/SimpleMarker"], function (SimpleMarker) {
                                item = new SimpleMarker({
                                    iconLabel: {
                                        innerHTML: index,
                                        style: {
                                            color: "#fff"
                                        }
                                    },
                                    iconStyle: "#1995f5",
                                    map,
                                    position: item.position
                                });
                            });
                        });
                    }
                },
                lng: 0,
                lat: 0,
                markers: [],
                tip: {
                    zIndex: 100
                },
                currentWindow: {
                    position: [121.5273285, 31.21515044],
                    content: "",
                    visible: false,
                    offset: [1.5, -35],
                    events: {
                        close () {
                        }
                    }
                }
            };
        },
        watch: {
            hotelDetailList: {
                deep: true,
                handler (newV) {
                    if (newV.length) {
                        if (this.isAround) {
                            this.processing_data_format_for_around();
                        } else {
                            this.processing_data_format_for_hotel();
                        }
                        this.get_map_center();
                        this.map_show();
                    } else {
                        this.markers.length = 0;
                        this.loading_completed = false;
                        setTimeout(() => {
                            this.loading_completed = true;
                        }, 500);
                    }
                }
            },
            center_index (newV) {
                let list = []
                this.markers[newV].position.forEach(v=>{list.push(v)})
                this.center = list
                this.currentWindow.content = this.markers[newV].title
                this.currentWindow.position = list
                this.currentWindow.visible = true
            }
        },
        methods: {
            get_map_center () {
                const positionXList = [];
                const positionYList = [];
                this.markers.forEach((Element) => {
                    positionXList.push(Element.position[0]);
                    positionYList.push(Element.position[1]);
                });
                const X = (Math.max(...positionXList) + Math.min(...positionXList)) / 2;
                const Y = (Math.max(...positionYList) + Math.min(...positionYList)) / 2;
                this.center = [X, Y];
            },
            processing_data_format_for_hotel () {
                const hotelList = this.hotelDetailList.map((Element, index) => {
                    const position = [Element.location.longitude, Element.location.latitude];
                    return {
                        position,
                        events: {
                            mouseover: () => {
                                this.currentWindow.content = Element.name;
                                this.currentWindow.position = position;
                                this.currentWindow.visible = true;
                            },
                            mouseout: () => {
                                this.currentWindow.visible = false;
                            },
                            click: () => {
                                this.center = position;
                                this.tip.zIndex++;
                                this.markers[index].zIndex = this.tip.zIndex;
                            }
                        },
                        visible: true,
                        draggable: false,
                        content: `<span class="marker" id="${index}">${index + 1}</span>`,
                        title: `${Element.name}`,
                        zIndex: 100
                    };
                });
                this.markers = hotelList;
            },
            processing_data_format_for_around () {
                //this.hotelDetailList.push(this.hotel_detail)
                const hotelList = this.hotelDetailList.map((Element, index) => {
                    if(Element.name === this.hotel_detail.name) {
                        const position = [Element.location.longitude, Element.location.latitude];
                        return {
                            position,
                            events: {
                                mouseover: () => {
                                    this.currentWindow.content = Element.name;
                                    this.currentWindow.position = position;
                                    this.currentWindow.visible = true;
                                    this.center = position;
                                    this.tip.zIndex++;
                                    this.markers[index].zIndex = this.tip.zIndex;
                                },
                                mouseout: () => {
                                    this.currentWindow.visible = false;
                                },
                                click: () => {
                                    this.center = position;
                                    this.tip.zIndex++;
                                    this.markers[index].zIndex = this.tip.zIndex;
                                }
                            },
                            visible: true,
                            draggable: false,
                            content: `<span class="marker_hotel" id="${index}"></span>`,
                            title: `${Element.name}`,
                            zIndex: 99-index
                        }
                    }else {
                        const position = [Element.location.split(",")[0], Element.location.split(",")[1]];
                        return {
                            position,
                            events: {
                                mouseover: () => {
                                    this.currentWindow.content = Element.name;
                                    this.currentWindow.position = position;
                                    this.currentWindow.visible = true;
                                    this.center = position;
                                    this.tip.zIndex++;
                                    this.markers[index].zIndex = this.tip.zIndex;
                                },
                                mouseout: () => {
                                    this.currentWindow.visible = false;
                                },
                                click: () => {
                                    this.center = position;
                                    this.tip.zIndex++;
                                    this.markers[index].zIndex = this.tip.zIndex;
                                }
                            },
                            visible: true,
                            draggable: false,
                            content: `<span class="marker" id="${index}">${index + 1}</span>`,
                            title: `${Element.name}`,
                            zIndex: 99-index
                        };

                    }
                });
                this.markers = hotelList;
            },
            map_show () {
                if (typeof (AMapUI) === "undefined") {
                    setTimeout(() => {
                        this.map_show();
                    }, 100);
                } else {
                    this.loading_completed = true;
                    // 缩放
                    setTimeout(() => {
                        const o = amapManager.getMap();
                        // o.setZoomAndCenter(12);
                        o.setFitView();
                        //o.getCenter();
                    }, 1000);
                }
            }
        }
    };
</script>

<style lang="less" scoped>
.map_container {
    width: 100%;
    height: 100%;
    .map, .loading {
        width: 100%;
        height: 100%;
    }
}
/deep/.marker {
    display: inline-block;
    width: 22px;
    height: 36px;
    background-image: url(https://webapi.amap.com/theme/v1.3/markers/b/mark_b.png);
    background-size: 22px 36px;
    text-align: center;
    line-height: 24px;
    color: #fff;
}
/deep/ .marker_hotel {
    display: inline-block;
    width: 22px;
    height: 36px;
    background: url('../../static/hotel_7.png');
    background-size: 22px 36px;
}
/deep/.amap-simple-marker  {
    display: none;
}
</style>
